home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: mxsld2.pd.infn.it!LORETI
- From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
- Subject: Re: File Size!
- X-Nntp-Posting-Host: mxsld2.pd.infn.it
- Message-ID: <DLv6tr.KGL@news.cern.ch>
- Sender: news@news.cern.ch (USENET News System)
- Reply-To: loreti@mxsld2.pd.infn.it
- Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
- References: <4e11ph$733@hammerhead.dadd.ti.com>,<3104B32D.74C9@fokus.gmd.de>
- Date: Sun, 28 Jan 1996 00:03:25 GMT
-
- In article <3104B32D.74C9@fokus.gmd.de>, Watson <sayegh@fokus.gmd.de> writes:
- >Sudheer Vemulapalli wrote:
- >>
- >> Hello Netters,
- >>
- >> Is there a simple way to find the exact size of a file. I would like to know of
- >> a way to do it other than using ls -l and getting the size field from the
- >> output. If there is a UNIX system call or somefunction that returns the file
- >> size please let me know.
- >
- >You need to open the file:
- >
- >int fd = open(<filename>, O_RDONLY);
- >
- >Then you go to the end of the file:
- >
- >size = lseek(fd, 0, SEEK_END);
- >
- >The result of this op is already the size.
- >This is a quite portable, but slow way.
- >--
- >* Greetinx from Watson (sayegh@fokus.gmd.de)
- > http://www.fokus.gmd.de/ovma/employees/sayegh/entry.html
- > signal(SIGSEGV,SIG_IGN);
-
- From the FAQ list:
-
- 19.12: How can I find out the size of a file, prior to reading it in?
-
- A: If the "size of a file" is the number of characters you'll be
- able to read from it in C, it is difficult or impossible to
- determine this number exactly).
-
- Under Unix, the stat() call will give you an exact answer.
- Several other systems supply a Unix-like stat() which will give
- an approximate answer. You can fseek() to the end and then use
- ftell(), but these tend to have the same problems: fstat() is
- not portable, and generally tells you the same thing stat()
- tells you; ftell() is not guaranteed to return a byte count
- except for binary files. Some systems provide routines called
- filesize() or filelength(), but these are not portable, either.
-
- Are you sure you have to determine the file's size in advance?
- Since the most accurate way of determining the size of a file as
- a C program will see it is to open the file and read it, perhaps
- you can rearrange the code to learn the size as it reads.
-
- References: ANSI Sec. 4.9.9.4; ISO Sec. 7.9.9.4; H&S
- Sec. 15.5.1; PCS Sec. 12 p. 213; POSIX Sec. 5.6.2.
-
- Hope that helps... (why so few people care to read the FAQ before
- posting/replying?)
- --
- Maurizio Loreti http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
- Un. of Padova, Dept. of Physics - Padova, Italy loreti@padova.infn.it
-